package com.tly.common.controller;

import com.tly.common.util.MinioUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author tly12
 */
@RestController
@Tag(name = "minio", description = "minio controller")
@RequestMapping("/test/")
public class MinioFileController {

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 单文件上传
     *
     * @param file 文件
     * @return
     */
    @PostMapping(name = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "文件上传")
    public Object uploadOne(@RequestPart MultipartFile file) {
        return minioUtil.putObject(file);
    }

    /**
     * 单文件下载
     *
     * @param filepath 文件路径
     * @param response 下载完后是否删除, 请谨慎传参
     */
    @GetMapping("/download")
    @Operation(summary = "文件下载")
    public void download(@RequestParam(value = "filepath") String filepath,
                         HttpServletResponse response) {
        minioUtil.getObject(response, filepath);
    }

    /**
     * 获取全部桶名称
     *
     * @return
     */
    @GetMapping("/bucket/list")
    @Operation(summary = "获取全部桶")
    public List<Map<String, Object>> getAllBuckets() {
        return minioUtil.getAllBuckets();
    }

    /**
     * 判断桶是否存在
     *
     * @return
     */
    @GetMapping("/bucket/exist")
    @Operation(summary = "判断桶是否存在", description = "如果不存在就创建")
    public Boolean getBucketByName(@RequestParam(value = "桶名称") String bucketName) {
        return minioUtil.existBucket(bucketName);
    }

    /**
     * 文件预览
     *
     * @return
     */
    @GetMapping("/preview")
    @Operation(summary = "文件预览")
    public String preview(@RequestParam(value = "文件名称") String fileName) {
        return minioUtil.preview(fileName);
    }

    /**
     * 文件预览
     *
     * @return
     */
    @GetMapping("/delete")
    @Operation(summary = "文件预览")
    public Boolean delete(@RequestParam(value = "文件名称") String fileName) {
        return minioUtil.remove(fileName);
    }

    /**
     * 查看存储的文件列表
     *
     * @param bucketName 桶
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "文件列表")
    public List<Map<String, Object>> fileList(@Parameter(description = "桶名称") @RequestParam(required = false) String bucketName) {
        return minioUtil.listObjects(bucketName);
    }

    /**
     * 删除文件
     *
     * @param bucketName 桶
     * @param list 文件名列表
     * @return
     */
    @DeleteMapping("/removeList")
    @Operation(summary = "批量删除文件")
    public Map<String, Object> removeList(@Parameter(description = "桶名称") @RequestParam(required = false) String bucketName,
                                      @Parameter(description = "文件名集合") @RequestParam List<String> list) {
        return minioUtil.removeList(bucketName, list);
    }
}
